

#include "JsonDataModel.h"
#include "JsonReader.h"
#include "cJSON.h"
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string.h>
#include <string>

using namespace std;

vector<struct JsonDataModel> getJsonDataModelFromFile(char* filename)
{
	JsonReader* reader = new JsonReader(filename);
	return getJsonDataModel(reader);
}

vector<struct JsonDataModel> getJsonDataModelFromString(char* data)
{
	JsonReader* reader = new JsonReader();
	reader->setData(data);
	return getJsonDataModel(reader);
}

vector<struct JsonDataModel> getJsonDataModel(JsonReader* reader)
//JsonDataModel getJsonDataModel(char* filename)
{
	vector<struct JsonDataModel> models;
	//JsonDataModel model;
	//JsonReader reader(filename);

	int index = 1;
	while(1) {
		if(!strcmp("", reader->getData((char*)to_string(index).c_str()))) {
			break;
		}
		JsonDataModel model;
		model.settings.merge_type = reader->getData((char*)(to_string(index)+" settings merge_type").c_str());
		model.settings.file_name = reader->getData((char*)(to_string(index)+" settings file_name").c_str());
		model.settings.set_quantity_to_zero = reader->getData("1 settings set_quantity_to_zero");

	        model.columns.name = reader->getData((char*)(to_string(index)+" columns name").c_str());
        	model.columns.article = reader->getData((char*)(to_string(index)+" columns article").c_str());
	        model.columns.code = reader->getData((char*)(to_string(index)+" columns code").c_str());
        	model.columns.price = reader->getData((char*)(to_string(index)+" columns price").c_str());
        	model.columns.rrp = reader->getData((char*)(to_string(index)+" columns rrp").c_str());
        	int q_size = reader->getArraySize((char*)(to_string(index)+" columns quantity").c_str());
        	for(int i = 0; i < q_size; i++) {
        		string quantity(reader->getData((char*)(to_string(index)+" columns quantity ###").c_str(),i));
        		model.columns.quantity.push_back(quantity);
        	}

        	model.columns.comment = reader->getData((char*)(to_string(index)+" columns comment").c_str());
        	q_size = reader->getArraySize((char*)(to_string(index)+" columns more_comment").c_str());
        	for(int i = 0; i < q_size; i++) {
        		string more_comment(reader->getData((char*)(to_string(index)+" columns more_comment ###").c_str(),i));
        		model.columns.more_comment.push_back(more_comment);
        	}

        	if(!strcmp("", reader->getData((char*)(to_string(index)+" relative_columns").c_str()))) {
    	        model.relative_columns.name = reader->getData((char*)(to_string(index)+" relative_columns name").c_str());
            	model.relative_columns.article = reader->getData((char*)(to_string(index)+" relative_columns article").c_str());
    	        model.relative_columns.code = reader->getData((char*)(to_string(index)+" relative_columns code").c_str());
            	model.relative_columns.price = reader->getData((char*)(to_string(index)+" relative_columns price").c_str());
            	model.relative_columns.rrp = reader->getData((char*)(to_string(index)+" relative_columns rrp").c_str());
            	q_size = reader->getArraySize((char*)(to_string(index)+" relative_columns quantity").c_str());
            	for(int i = 0; i < q_size; i++) {
            		string relative_quantity(reader->getData((char*)(to_string(index)+" relative_columns quantity ###").c_str(),i));
            		model.relative_columns.quantity.push_back(relative_quantity);
            	}
            	model.relative_columns.comment = reader->getData((char*)(to_string(index)+" relative_columns comment").c_str());

            	q_size = reader->getArraySize((char*)(to_string(index)+" relative_columns more_comment").c_str());
            	for(int i = 0; i < q_size; i++) {
            		string relative_more_comment(reader->getData((char*)(to_string(index)+" relative_columns more_comment ###").c_str(),i));
            		model.relative_columns.more_comment.push_back(relative_more_comment);
            	}
        	}
        	else{
    	        model.relative_columns.name = "";
        	}


	        model.custom_values.name = reader->getData((char*)(to_string(index)+" custom_values name").c_str());
        	model.custom_values.article = reader->getData((char*)(to_string(index)+" custom_values article").c_str());
	        model.custom_values.code = reader->getData((char*)(to_string(index)+" custom_values code").c_str());
        	model.custom_values.price = reader->getData((char*)(to_string(index)+" custom_values price").c_str());
        	model.custom_values.rrp = reader->getData((char*)(to_string(index)+" custom_values rrp").c_str());
	        model.custom_values.quantity = reader->getData((char*)(to_string(index)+" custom_values quantity").c_str());
        	model.custom_values.comment = reader->getData((char*)(to_string(index)+" custom_values comment").c_str());

        	int sm_size = reader->getArraySize((char*)(to_string(index)+" service_marks").c_str());
		for(int i = 0; i < sm_size; i++) {
			struct Service_marks sm;

			sm.main["type"] = reader->getData((char*)(to_string(index)+" service_marks ### main type").c_str(), i);
			sm.main["source"] = reader->getData((char*)(to_string(index)+" service_marks ### main source").c_str(), i);
			sm.main["target"] = reader->getData((char*)(to_string(index)+" service_marks ### main target").c_str(), i);
			sm.main["get_first"] = reader->getData((char*)(to_string(index) + " service_marks ### main get_first").c_str(), i);
			sm.main["name"] = reader->getData((char*)(to_string(index)+" service_marks ### main name").c_str(), i);
			sm.main["article"] = reader->getData((char*)(to_string(index)+" service_marks ### main article").c_str(), i);
			sm.main["code"] = reader->getData((char*)(to_string(index)+" service_marks ### main code").c_str(), i);
			sm.main["price"] = reader->getData((char*)(to_string(index)+" service_marks ### main price").c_str(), i);
			sm.main["quantity"] = reader->getData((char*)(to_string(index)+" service_marks ### main quantity").c_str(), i);
			sm.main["rrp"] = reader->getData((char*)(to_string(index)+" service_marks ### main rrp").c_str(), i);
			sm.main["comment"] = reader->getData((char*)(to_string(index)+" service_marks ### main comment").c_str(), i);

			sm.extention["name"] = reader->getData((char*)(to_string(index)+" service_marks ### exception name").c_str(), i);
			sm.extention["article"] = reader->getData((char*)(to_string(index)+" service_marks ### exception article").c_str(), i);
			sm.extention["code"] = reader->getData((char*)(to_string(index)+" service_marks ### exception code").c_str(), i);
			sm.extention["price"] = reader->getData((char*)(to_string(index)+" service_marks ### exception price").c_str(), i);
			sm.extention["quantity"] = reader->getData((char*)(to_string(index)+" service_marks ### exception quantity").c_str(), i);
			sm.extention["rrp"] = reader->getData((char*)(to_string(index)+" service_marks ### exception rrp").c_str(), i);
			sm.extention["comment"] = reader->getData((char*)(to_string(index)+" service_marks ### exception comment").c_str(), i);

			model.service_marks.push_back(sm);

		}
		models.push_back(model);
		index++;
	}

/*
	model.settings.merge_type = reader.getData("1 settings merge_type");
	model.settings.file_name = reader.getData("1 settings file_name");
	model.settings.set_quantity_to_zero = reader.getData("1 settings set_quantity_to_zero");

	model.columns.name = reader.getData("1 columns name");
	model.columns.article = reader.getData("1 columns article");
	model.columns.code = reader.getData("1 columns code");
	model.columns.price = reader.getData("1 columns price");
	model.columns.quantity = reader.getData("1 columns quantity");
	model.columns.comment = reader.getData("1 columns comment");

	model.service_marks.main.type = reader.getData("1 service_marks ### main type");
	model.service_marks.main.source = reader.getData("1 service_marks ### main source");
	model.service_marks.main.article = reader.getData("1 service_marks ### main article");
	model.service_marks.main.price = reader.getData("1 service_marks ### main price");
	model.service_marks.main.price = reader.getData("1 service_marks ### main price");
	model.service_marks.extention.article = reader.getData("1 service_marks ### exception article");
*/
	return models;
}
